﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Collections.Generic;

namespace Linkmeme
{
    public class SourceViewData<TSource> where TSource : SourceBase
    {
        List<TSource> _Items;
        public List<TSource> Items
        {
            get
            {
                if (_Items == null)
                    _Items = new List<TSource>();
                return _Items;
            }

            set
            {
                _Items = value;
            }
        }

        public string ProviderName 
        {
            get
            {
                if (Items != null && Items.Count > 0)
                    return Items[0].ProviderName;
                else
                    return "";
            }    
        }

        public List<GroupItemData<TSource>> GroupData()
        {
            if (string.IsNullOrEmpty(this.GroupType))
                this.GroupType = "Provider";
            return GroupData(this.GroupType);
        }

        public List<GroupItemData<TSource>> GroupData(string groupType)
        {
            List<TSource> items = this.Items;
            IEnumerable<GroupItemData<TSource>> groupedData = null;
            switch (groupType)
            {
                case "Provider":
                    groupedData = from i in items
                                         group i by i.ProviderName into groups
                                         select new GroupItemData<TSource>
                                         {
                                             GroupKey = groups.Key,
                                             GroupType = "Provider",
                                             Items = groups.ToList()
                                         };
                    break;

                case "UserName":
                    groupedData = from i in items
                                         group i by i.UserName into groups
                                         select new GroupItemData<TSource>
                                         {
                                             GroupKey = groups.Key,
                                             GroupType = "UserName",
                                             Items = groups.ToList()
                                         };
                    break;
                case "PubDate":
                    groupedData = from i in items
                                         group i by i.PubDate.ToString("yyy MMM") into groups
                                         select new GroupItemData<TSource>
                                         {
                                             GroupKey = groups.Key,
                                             GroupType = "PubDate",
                                             Items = groups.ToList()
                                         };
                    break;

                default:
                    break;
            }
            return groupedData.ToList();
        }

        public string GroupType { get; set; }
        public string SourceType 
        {
            get
            {
                string t = typeof(TSource).ToString();
                t = t.Substring(t.LastIndexOf(".") + 1);
                return t == "Picture" ? "Photo" : t;
            }
        }
    }


}
